package es.cea.tareas;

import java.io.IOException;
import java.io.PrintWriter;
import java.util.List;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

import es.cea.tareas.model.Tarea;

/**
 * Servlet implementation class EditaTarea
 */
public class EditaTarea extends HttpServlet {
	private static final long serialVersionUID = 1L;
       
	Log logger=LogFactory.getLog(this.getClass());

	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		String parameter = request.getParameter("idTarea");
		int idTarea=Integer.parseInt(parameter);
		Object attribute = request.getSession().getServletContext().getAttribute("tareas");
		List<Tarea> tareas=(List<Tarea>) attribute;
		Tarea tarea=encuentraTarea(tareas, idTarea);
		PrintWriter writer = response.getWriter();
		writer.print("<h1>EDITANDO TAREA</h1>");
		writer.print("<form action='./confirmaEdicionTarea' method='POST'>" +
				"<input type='text' name='titulo' value='"+tarea.titulo+"'>" +
				"<input type='text' name='descripcion' value='"+tarea.descripcion+"'>" +
				"<input type='hidden' name='idTarea' value='"+tarea.id+"'>" +
				"<input type='submit' value='editar'>" +
				"</form>");
		
	}

	private Tarea encuentraTarea(List<Tarea> tareas, int idTarea) {
		for(Tarea t:tareas)
			if(t.id==idTarea) return t;
		throw new RuntimeException("la tarea con el id:"+idTarea+" NO EXISTE!!");
	}

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
	}

}
